package com.flats.webview;

import com.flats.model.entity.Comment;
import com.flats.model.entity.Flat;
import com.flats.model.entity.Users;
import com.flats.service.transactional.TransactionalCommentService;
import com.flats.service.transactional.TransactionalFlatService;
import com.flats.utils.Constants;
import com.google.common.base.Functions;
import com.google.common.collect.Ordering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class FlatDetailController {

    @Autowired
    TransactionalFlatService flatService;

    @Autowired
    TransactionalCommentService commentService;

    @RequestMapping(value = "/flat.detail")
    public String viewFlatDetail(Model model,@RequestParam(value = "flatId") String flatId) {
        Flat flat = flatService.get(Long.valueOf(flatId));
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Map<Long,String> commentsDate=new HashMap<Long, String>();
        for (Comment comment : flat.getComments()) {
            commentsDate.put(comment.getId(), df.format(comment.getDate()));
        }
        model.addAttribute("flat",flat);
        model.addAttribute("commantsDate",commentsDate);
        return "flatDetail";
    }

    @RequestMapping(value = "/comment.add")
    public String  commentSaving(Model model,@RequestParam(value = "comment") String comment,@RequestParam(value = "flatId") String flatId,@RequestParam(value = "date") String date) throws ParseException {
        System.out.println("flatId = " + flatId);
        System.out.println("date = " + date);
        Date dateForSave = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse(date);
        System.out.println("dateForSave = " + dateForSave);
        Comment newComment=new Comment();
        newComment.setText(comment);
        Users session = (Users) Utils.getSession(Constants.USER);
        newComment.setUser(session);
        newComment.setDate(dateForSave);
        Flat flat = flatService.get(Long.valueOf(flatId));
        newComment.setFlat(flat);
        System.out.println("flat stored");
        commentService.saveOrUpdate(newComment);


        flat = flatService.get(Long.valueOf(flatId));

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Map<Long,String> commentsDate=new HashMap<Long, String>();
        for (Comment commentEl : flat.getComments()) {
            commentsDate.put(commentEl.getId(), df.format(commentEl.getDate()));
        }
        model.addAttribute("flat",flat);
        model.addAttribute("commantsDate",commentsDate);
        return "comments";
    }
}
